/**
 * jims
 */
package com.jims.master.modules.doctor.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.NumberUtils;
import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.dao.*;
import com.jims.master.modules.doctor.entity.*;
import com.jims.master.modules.doctor.vo.SaveDataVo;
import com.jims.master.modules.finance.dao.InpBillDetailDao;
import com.jims.master.modules.finance.entity.InpBillDetail;
import com.jims.master.modules.oper.dao.ClinicItemDictDao;
import com.jims.master.modules.oper.dao.ClinicVsChargeDao;
import com.jims.master.modules.oper.dao.PriceListDao;
import com.jims.master.modules.oper.entity.ClinicItemDict;
import com.jims.master.modules.oper.entity.PriceList;
import com.jims.master.modules.orders.dao.OrdersDao;
import com.jims.master.modules.orders.entity.Orders;
import com.jims.master.modules.orders.service.OrdersService;
import com.jims.master.modules.sys.dao.CompanyDao;
import com.jims.master.modules.sys.dao.OrgDeptDao;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 检查申请Service
 * @author 陈晓阳
 * @version 2017-03-14
 */
@Service
@Transactional(readOnly = true)
public class ExamApplyService extends CrudService<ExamApplyDao, ExamApply> {

    @Autowired
    private ExamApplyDao examApplyDao;//检查申请Dao
    @Autowired
    private ExamItemDao examItemDao;//检查申请明细Dao
    @Autowired
    private ClinicItemDictDao clinicItemDictDao;//诊疗项目Dao
    @Autowired
    private OutpTreatRecDao outpTreatRecDao;//处置治疗Dao
    @Autowired
    private PriceListDao priceListDao;
    @Autowired
    private ExamItemService examItemService;//检查申请明细Dao
    @Autowired
    private ClinicOrdersDao clinicOrdersDao;
    @Autowired
    private InpBillDetailDao inpBillDetailDao;
    @Autowired
    private ClinicOrdersCostService clinicOrdersCostService;//门诊医嘱收费明细service
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private PatVisitService patVisitService;
    @Autowired
    private ClinicVsChargeDao clinicVsChargeDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private ClinicMasterDao clinicMasterDao;
    @Autowired
    private HisDiagnosisDao hisDiagnosisDao;
    @Autowired
    private OrgDeptDao orgDeptDao;
    @Autowired
    private PatVisitDao patVisitDao;
    @Autowired
    private ClinicMasterService clinicMasterService;//门诊就诊记录service
    @Autowired
    private OrdersDao ordersDao;

	public ExamApply get(String id) {
		return super.get(id);
	}

	public List<ExamApply> findList(ExamApply examApply) {
		return super.findList(examApply);
	}

	public Page<ExamApply> findPage(Page<ExamApply> page, ExamApply examApply) {
		return super.findPage(page, examApply);
	}

	@Transactional(readOnly = false)
	public int save(ExamApply examApply) {

		return super.save(examApply);
	}

    /**
     * 删除检查申请
     * @param examApply
     * @return
     * @Author 陈晓阳
     */
	@Transactional(readOnly = false)
	public int delete(ExamApply examApply) {
        int resultVal=0;
        examApply=this.examApplyDao.get(examApply.getId());
        String ordersId=examApply.getOrdersId();
        commonDel(ordersId);
        resultVal+=examItemService.deleteByApplyId(examApply.getId());
        examApplyDao.delete(examApply);
        return resultVal;
	}

    /**
     * 删除代收费和处置治疗
     * @param ordersId
     * @return
     * @Author 陈晓阳
     */
    @Transactional(readOnly = false)
    public int commonDel(String ordersId){
        int resultVal=0;
        resultVal+=this.clinicOrdersCostService.delByOrderId(ordersId);
        resultVal+=this.outpTreatRecDao.deleteByOrdersId(ordersId);
        resultVal+=this.clinicOrdersDao.delete(this.clinicOrdersDao.get(ordersId));
        return resultVal;
    }

    /**
     * 根据Id删除对象
     * @param examApplyId
     * @return
     * @Author 陈晓阳
     */
    @Transactional(readOnly = false)
    private int deleteExamApplyById(String examApplyId) {
        return this.examApplyDao.deleteById(examApplyId);
    }


    /**
     * 查询此个病人的所有待做和未做的检查项目
     * @return
     * @Author 陈晓阳
     */
    public Page<ExamItem> queryExamByPatient(Page<ExamItem> examItemPage, ExamItem examItem) {
        examItem.updateOrgId();
        examItem.setPage(examItemPage);
        List<ExamItem> examItems= this.examItemDao.queryExamByPatient(examItem.getExamApply().getPatientId(), examItem.getOrgId(), examItem.getItemName(), examItemPage);
        examItemPage.setList(examItems);
        return examItemPage;
    }
    /**
     * 保存当前患者新开的检查申请
     * @return
     * @Author 陈晓阳
     */
    @Transactional(readOnly = false)
    public int saveExamApplyData(ExamItem examItem) {
        User user= UserUtils.getUser();
        Integer rows=0;
        //1 保存检查申请zhu表
        ExamApply examApply=saveExamApply(examItem);
        examItem.setExamApply(examApply);
        //2 保存子表
        List<SaveDataVo> saveDataVos=saveExamItems(examItem);//多条记录 对应诊疗项目
        rows=saveDataVos.size();
        if("0".equals(examApply.getInoroutFlag())){
            //将clinicmaster里面的科室添加到saveDataVos实体
            ClinicMaster clinicMaster=clinicMasterService.get(examApply.getClinicId());
            String doctorId=clinicMaster.getDoctorId();
            if(StringUtils.isBlank(doctorId)){
                clinicMaster.setDoctorId(user.getId());
                clinicMaster.setClinicType("1");
            }
            clinicMasterService.updateClinicType(clinicMaster);
            for(SaveDataVo saveDataVo : saveDataVos){
                saveDataVo.setApplyDept(clinicMaster.getDeptId());
            }
            //3保存处置治疗表和代收费表
            Map<String,Object> map=this.commonSave(saveDataVos);//多条记录 对应诊疗项目
            //更新examApplyorderssId
            examApply.setOrdersId((String) map.get("ordersId"));
            save(examApply);
            rows= (Integer) map.get("rows");
        }else {
            PatVisit patVisit=this.patVisitService.get(examItem.getExamApply().getVisitId());
             //住院
            String[] clincItemIds = examItem.getClinicItemIds().split(",");
            for (String clinicItemId : clincItemIds) {
                ClinicItemDict clinicItemDict = this.clinicItemDictDao.get(clinicItemId);
                Orders saveData = new Orders();
                saveData.setApplyId(examItem.getExamApply().getId());
                saveData.setOrderText(clinicItemDict.getItemName());
                saveData.setOrderClass(clinicItemDict.getItemClass());
                saveData.setClinicItemId(clinicItemDict.getId());
                saveData.setBillingAttr("0");
                saveData.setOrderStatus("1");//传输状态
                saveData.setWardCode(patVisit.getWardCode());
                saveData.setPatientId(examItem.getExamApply().getPatientId());
                saveData.setPerformedBy(examItem.getExamApply().getImplementDept());
                saveData.setVisitId(examItem.getExamApply().getVisitId());
                saveData.setRepeatIncaditor("0");
                saveData.setIsApply("1");
                saveData.setNewDept(examItem.getExamApply().getBillingDept());
                ordersService.save(saveData);
            }
        }
        return rows;
    }




    /**
     * 保存病人申请检查子表记录多条数据
     * @param examItem
     * @return
     * @Author 陈晓阳
     */
    private List<SaveDataVo> saveExamItems(ExamItem examItem){
        List<SaveDataVo> resultVal=new ArrayList<SaveDataVo>();
        String[] clincItemIds=examItem.getClinicItemIds().split(",");
        for(String clinicItemId:clincItemIds){
            ClinicItemDict clinicItemDict=this.clinicItemDictDao.get(clinicItemId);
            ExamItem saveData=new ExamItem();
            saveData.setApplyId(examItem.getExamApply().getId());
            saveData.setItemName(clinicItemDict.getItemName());
            saveData.setItemCode(clinicItemDict.getItemCode());
            saveData.setClinicItemId(clinicItemDict.getId());
            double price=this.clinicItemDictDao.findPriceById(clinicItemDict.getId());
            saveData.setCosts(price+"");
            this.examItemService.save(saveData);
            SaveDataVo saveDataVo=new SaveDataVo();
            saveDataVo.setPatientId(examItem.getExamApply().getPatientId());
            saveDataVo.setItemName(clinicItemDict.getItemName());
            saveDataVo.setImplementDept(examItem.getExamApply().getImplementDept());
            saveDataVo.setChargeFlag(examItem.getExamApply().getChargeFlag());
            saveDataVo.setConfirmFlag(examItem.getExamApply().getConfirmFlag());
            saveDataVo.setClinicId(examItem.getExamApply().getClinicId());
            saveDataVo.setNum(1);
            saveDataVo.setClinicItemId(clinicItemDict.getId());
            saveDataVo.setApplyId(examItem.getExamApply().getId());
            saveDataVo.setSumPrice(price);
            saveDataVo.setItemClass(clinicItemDict.getItemClass());
            saveDataVo.setIsApply("1"); // 是否申请单
            saveDataVo.setApplyNo(examItem.getExamApply().getApplyOn());
            saveDataVo.setRcptNo("");//收费号
            saveDataVo.setImplementUser("");//执行人
            saveDataVo.setInoroutFlag(examItem.getExamApply().getInoroutFlag());
            resultVal.add(saveDataVo);
        }
        return resultVal;
    }

    /**
     *保存病人申请检查zhu表记录一条数据
     * @param examItem
     * @return
     * @Author 陈晓阳
     */
    private ExamApply saveExamApply(ExamItem examItem){
        ExamApply examApply=examItem.getExamApply();
        if("0".equals(examApply.getInoroutFlag())){
            examApply.setApplyOn(NumberUtils.getClinicInspect(examApply.getClinicId()));//申请序号
        }else{
            examApply.setApplyOn(NumberUtils.getClinicInspectIn(examApply.getVisitId()));//申请序号
        }
        examApply.setConfirmFlag("0");//确认标志
        examApply.setChargeFlag("0");
        examApply.setBillingDept(UserUtils.getUser().getOrgDept().getId());//开单科室
        this.save(examApply);
        return examApply;
    }

    /**
     * 检查单打印
     * @param examApply
     * @param user
     * @return
     * @author CTQ
     */
    public String printExamApply(ExamApply examApply,User user){
        String orderedBy = orgDeptDao.get(user.getOrgDept()!=null?user.getOrgDept().getId():"").getDeptName();//护理单元
        String userName = user.getName();//打印人
        Map map =new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("hospital", UserUtils.getUser().getCompany().getOrgName());
        map.put("orderedBy", orderedBy);
        map.put("userName",userName);
        map.put("date", sdf.format(new Date()));
        List<BaseDto> baseDtoList = examItemDao.printExamApply(user.getOrgDept().getId());
        String printPath = PrintPdfUtils.previewPdf(user.getId(), "his/nurse/nursesExam.jasper", map, baseDtoList, "nursesExam");
        return printPath;
    }

    /**
     * <p>
     *     公共方法
     * </p>
     * @Param saveDataVos
     * 保存处置治疗表和代收费表
     * @return
     * @Author 陈晓阳
     */
    public Map<String,Object> commonSave(List<SaveDataVo> saveDataVos) {
        Map<String,Object> map=new HashMap<String, Object>();
        int resultVal=0;
        //1 保存代收费表 clinic_orders
        ClinicOrders cLinicOrders=this.saveClinicOrders(saveDataVos.get(0));
        //2 clinic_orders_cost
        resultVal+=this.saveClinicOrdersCosts(saveDataVos, cLinicOrders.getId());
       //3 保存处置治疗数据
        String treatId=this.saveOutoTreatRec(saveDataVos,cLinicOrders.getId());
        map.put("rows",resultVal);
        map.put("ordersId",cLinicOrders.getId());
        map.put("treatId",treatId);
        return map;
    }
    /**
     * 保存处置治疗数据
     * @param saveDataVos
     * @param ordersId clinic_orders 主键
     * @return
     */
    private String saveOutoTreatRec(List<SaveDataVo> saveDataVos, String ordersId) {
        String resultVal="";
        for(SaveDataVo saveDataVo:saveDataVos){
            OutpTreatRec outpTreatRec=new OutpTreatRec();
            outpTreatRec.setInoroutFlag(saveDataVo.getInoroutFlag());
            outpTreatRec.setPatientId(saveDataVo.getPatientId());
            outpTreatRec.setClinicId(saveDataVo.getClinicId());
            outpTreatRec.setItemName(saveDataVo.getItemName());
            outpTreatRec.setChargeFlag(saveDataVo.getChargeFlag());
            outpTreatRec.setConfirmFlag(saveDataVo.getConfirmFlag());
            outpTreatRec.setClinicItemId(saveDataVo.getClinicItemId());
            outpTreatRec.setSupplierName(saveDataVo.getSupplierName());
            outpTreatRec.setPackageSpec(saveDataVo.getPackageSpec());
            outpTreatRec.setUnitsStr(saveDataVo.getUnitsStr());
            outpTreatRec.setImplementDept(saveDataVo.getImplementDept());
            outpTreatRec.setNum(saveDataVo.getNum() + "");
            outpTreatRec.setOrdersId(ordersId);
            outpTreatRec.setSumPrice(saveDataVo.getSumPrice() + "");
            outpTreatRec.setIsApply(saveDataVo.getIsApply());
            outpTreatRec.setItemClass(saveDataVo.getItemClass());
            outpTreatRec.preInsert();
            outpTreatRec.updateOrgId();
            this.outpTreatRecDao.insert(outpTreatRec);
            resultVal=outpTreatRec.getId();
        }
        return resultVal;
    }

    /**
     * 保存代收费明细-对应价表
     * @param saveDataVos
     * @param ordersId clinic_orders 主键
     * @return
     */
    private int saveClinicOrdersCosts(List<SaveDataVo> saveDataVos, String ordersId) {
        int resultVal=0;
        for(SaveDataVo saveDataVo:saveDataVos){
            //通过诊疗项目查找价表集合
            List<PriceList> baseDtoList = priceListDao.findListPrice(saveDataVo.getClinicItemId());
        //    List<BaseDto> baseDtoList=this.clinicItemDictDao.findVsChargeByClinicId(saveDataVo.getClinicItemId());
            for(PriceList baseDto:baseDtoList){
                ClinicOrdersCost cost=new ClinicOrdersCost();
                cost.setClinicId(saveDataVo.getClinicId());
                cost.setPatientId(saveDataVo.getPatientId());
                cost.setOrdersId(ordersId);
                cost.setItemClass(baseDto.getItemClass());
                cost.setItemName(baseDto.getItemName());
                cost.setItemCode(baseDto.getItemCode());
                cost.setPriceId(baseDto.getId());
                cost.setNum(Double.valueOf(baseDto.getAmount()*saveDataVo.getNum()));
                cost.setRcptNo(saveDataVo.getRcptNo());
                cost.setClassOnReckoning(baseDto.getClassOnReckoning());
                cost.setSubjCode(baseDto.getSubjCode());
                cost.setSpec(baseDto.getItemSpec());
                cost.setUnit(baseDto.getUnits());
                cost.setCoefficient(1.0);//收费系数
                resultVal+=this.clinicOrdersCostService.save(cost);
            }
        }
        return resultVal;
    }

    /**
     * 保存代收费主记录
     * @param saveDataVo
     * @return
     * @Author c陈晓阳
     */
    private ClinicOrders saveClinicOrders(SaveDataVo saveDataVo) {
        ClinicOrders clinicOrders=new ClinicOrders();
        clinicOrders.setImplementUser(saveDataVo.getImplementUser());
        clinicOrders.setRcptNo(saveDataVo.getRcptNo());
        clinicOrders.setApplyNo(saveDataVo.getApplyNo());
        clinicOrders.setApplyDept(saveDataVo.getApplyDept());
        clinicOrders.setImplementDept(saveDataVo.getImplementDept());
        clinicOrders.setClinicId(saveDataVo.getClinicId());
        clinicOrders.setPatientId(saveDataVo.getPatientId());
//        clinicOrders.setApplyId(saveDataVo.getApplyId());
        clinicOrders.setItemClass(saveDataVo.getItemClass());
        clinicOrders.setIsApply(saveDataVo.getIsApply());
        clinicOrders.setChargeFlag(saveDataVo.getChargeFlag());
        clinicOrders.preInsert();
        clinicOrders.updateOrgId();
        this.clinicOrdersDao.insert(clinicOrders);
        return clinicOrders;
    }

    /**
     * 执行确认操作
     * @param examApply
     * @return
     */
    @Transactional(readOnly = false)
    public int examConfirmDo(ExamApply examApply) {
        int sum = 0;
        if(examApply.getInoroutFlag() !=null && examApply.getInoroutFlag().equals("0")){
            OutpTreatRec outpTreatRec = new OutpTreatRec();
            outpTreatRec.setOrdersId(examApply.getOrdersId());
            outpTreatRecDao.ConfirmDo(outpTreatRec);
            sum =  this.examApplyDao.examConfirmDo(examApply.getId(),UserUtils.getUser().getId());
        }else if(examApply.getInoroutFlag() !=null && examApply.getInoroutFlag().equals("1")){
            Orders orders = new Orders();
            orders.setApplyId(examApply.getId());
            List<Orders> orderses= ordersDao.getOrders(orders);
           sum=this.examConfirmInHos(examApply,orderses);
           if(sum!=0){
               examApply.setConfirmUser(UserUtils.getUser().getId());
               examApplyDao.examConfirmInHos(examApply);
           }
        }
        return sum;
    }

    /**
     * 医技取消确认操作
     * @param examApply
     * @return int
     * @author 赵宁
     */
    @Transactional(readOnly = false)
    public int cancelExamConfirm(ExamApply examApply){
        int sum=0;
        if(examApply.getInoroutFlag() !=null && examApply.getInoroutFlag().equals("0")){
            OutpTreatRec outpTreatRec = new OutpTreatRec();
            outpTreatRec.setOrdersId(examApply.getOrdersId());
            outpTreatRecDao.cancelConfirmDo(outpTreatRec);
            sum =  this.examApplyDao.cancelExamConfirmDo(examApply.getId(),UserUtils.getUser().getId());
        }else if(examApply.getInoroutFlag() !=null && examApply.getInoroutFlag().equals("1")){
            Orders orders = new Orders();
            orders.setApplyId(examApply.getId());
            List<Orders> orderses = ordersDao.getOrders(orders);
            for(int i=0;i<orderses.size();i++){
                InpBillDetail inpBillDetail = new InpBillDetail();
                inpBillDetail.setOrdersId(orderses.get(i).getId());
                sum +=inpBillDetailDao.deleteInpDetails(inpBillDetail);//根据医嘱id删除收费明细记录
            }

            if(sum!=0){
                examApply.setCancelConfirmUser(UserUtils.getUser().getId());
                examApplyDao.cancelExamConfirmInHos(examApply);
            }
        }
        return sum;
    }
    /**
     * 检查确认--住院
     * @param examApply
     * @return
     * @author 赵宁
     */
    @Transactional(readOnly = false)
    public int examConfirmInHos(ExamApply examApply,List<Orders> orderses){
       int code=0;
       List<ExamItem> examItems=examItemDao.findAllItems(examApply.getId());
       if(examItems!=null && examItems.size()>0){
            for(int i=0;i<examItems.size();i++){
                 String orderId = orderses.get(i).getId();
                 String clinicItemId = examItems.get(i).getClinicItemId();
                 List<PriceList> priceLists = priceListDao.findListPrice(clinicItemId);
                 if(priceLists!=null && priceLists.size()>0){
                     for(int j=0;j<priceLists.size();j++){
                            PriceList priceList = priceLists.get(j);
                            InpBillDetail inpBillDetail=new InpBillDetail();
                            inpBillDetail.setOrdersId(orderId);
                            inpBillDetail.setVisitId(examApply.getVisitId());
                            inpBillDetail.setPatientId(examApply.getPatientId());
                            inpBillDetail.setPriceId(priceList.getId());
                            inpBillDetail.setItemClass(priceList.getItemClass());
                            inpBillDetail.setItemCode(priceList.getItemCode());
                            inpBillDetail.setItemName(priceList.getItemName());
                            inpBillDetail.setItemSpec(priceList.getItemSpec());
                            inpBillDetail.setAmount(priceList.getAmount() + "");
                            inpBillDetail.setUnits(priceList.getUnits());
                            inpBillDetail.setPriceQuotiety("0");
                            inpBillDetail.setItemPrice(priceList.getPrice());
                            inpBillDetail.setCosts(priceList.getPrice() * priceList.getAmount());
                            inpBillDetail.setCharges(priceList.getPrice() * priceList.getAmount());
                            inpBillDetail.setOrderDept(examApply.getBillingDept());
                            inpBillDetail.setOrderDoctor(examApply.getCreateBy().getId());
                            inpBillDetail.setPerformDept(examApply.getImplementDept());
                            inpBillDetail.setClassOnReckoning(priceList.getClassOnReckoning());
                            inpBillDetail.setSubjCode(priceList.getSubjCode());
                            inpBillDetail.setClassOnMr(priceList.getClassOnMr());
                            inpBillDetail.setOrgId(examApply.getOrgId());
                            inpBillDetail.setPriceId(priceList.getId());
                            inpBillDetail.setPerformPerson(UserUtils.getUser().getId());
                            inpBillDetail.setDeptStayed(UserUtils.getUser().getOrgDept().getId());
                            inpBillDetail.setWardCode(UserUtils.getUser().getOrgDept().getId());
                            inpBillDetail.preInsert();
                            code=inpBillDetailDao.insert(inpBillDetail);
                     }
                 }
            }
       }

        return code;
    }

    /**
     * 申请退费中
     * @param examApply
     * @return
     */
    @Transactional(readOnly = false)
    public int refundIng(ExamApply examApply){
        int num = examApplyDao.refundIng(examApply);
        return num;
    }

    /**
     * 护士端--未尽检查
     * @param visitId
     * @return
     * @author 赵宁
     */
    public List<ExamApply> getUnfinished(String visitId){
        return examApplyDao.getUnfinished(visitId);
    }

    /**
     * 门诊检查打印
     * @param examId
     * @param clinicId
     * @param patientId
     * @return
     */
    public String printPresc(String examId, String clinicId, String patientId,String visitId) {
        UserUtils userUtils = new UserUtils();
        Map map = new HashMap();
        String hospital = companyDao.get(userUtils.getUser().getOrgId()).getOrgName();
        ExamApply examApply = examApplyDao.get(examId);
        ClinicMaster clinicMaster = new ClinicMaster();
        String url = "";
        HisDiagnosis hisDiagnosis = new HisDiagnosis();
        if(!"".equals(clinicId) && clinicId != null){
            clinicMaster = clinicMasterDao.getPrintInfo(patientId);
            String sex =clinicMaster.getSex();
            map.put("sex",sex);
            map.put("age",clinicMaster.getAge());
            map.put("clinicNo",clinicMaster.getClinicNo());
            map.put("name",clinicMaster.getName());//患者姓名
            hisDiagnosis.setClinicId(clinicId);
            url = "his/exam/clinicExam/clinicExam.jasper";
        } else {
            PatVisit patVisit = patVisitDao.getPrintInfo(visitId);
            map.put("inpNo",patVisit.getVisitNo());
            map.put("bedNo",patVisit.getBedNo());
            map.put("name",patVisit.getPatVisitIndex().getName());//患者姓名
            map.put("sex",patVisit.getPatVisitIndex().getSex());
            map.put("age",patVisit.getPatVisitIndex().getAge());
            hisDiagnosis.setVisitId(visitId);
            url = "his/exam/hosExam/HosExam.jasper";
        }



        String hisDiagnosi = "";

        List<HisDiagnosis> list1 = hisDiagnosisDao.findList(hisDiagnosis);
        for(int i=0;i<list1.size();i++){
            hisDiagnosi+= list1.get(i).getIcdName()+",";
        }
        map.put("chubuzhenduan",hisDiagnosi);//临床诊断
        map.put("hospital",hospital);
        map.put("deptName",orgDeptDao.get(examApply.getBillingDept()).getDeptName());//申请科室
        map.put("userName",userUtils.getUser().getName());//申请医生
        map.put("performedBy",orgDeptDao.get(examApply.getImplementDept()).getDeptName()); // 执行科室
        map.put("examNo",examApply.getApplyOn());//申请号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        map.put("date",sdf.format(examApply.getCreateDate()));
//        if(electronEnterHospital1 != null){
//            if(!"".equals(electronEnterHospital1.getXianbingshi())){
//                map.put("xianbingshi", unescape(electronEnterHospital1.getXianbingshi()));
//            }else {
//                map.put("xianbingshi", "");
//            }
//        }else {
//            map.put("xianbingshi", "");
//        }
        map.put("physSign",examApply.getSign());//体征
        map.put("relevantDiag",examApply.getRelevantDiag());//其他诊断
        map.put("relevantLabTest",examApply.getRelevantResult());//相关化验结果
//        map.put("notice",examAppoints.getNotice());//注意事项
//        String examClass = examClassDictDao.get(examAppoints.getExamClass()).getExamClassName();
//        map.put("examClass",examClass);//检查类别
        List<BaseDto> baseDtoList = examItemDao.getExamItem(examId);
        String printPath = PrintPdfUtils.previewPdf(userUtils.getUser().getId(),url, map, baseDtoList, "clinicExam");
        return printPath;
    }

    /**
     * 住院退费申请
     * @param examItem
     * @return
     */
    @Transactional(readOnly = false)
    public int inpRetCharge(ExamApply examApply) {
        int num = 0;
        String applyId = examApply.getId();
        if("1".equals(examApply.getChargeFlag())){
            num =  dao.inpOutpRet("5",applyId); // 修改退费状态  3:已退；5申请
        } else {
        }
        return num;
    }

    /**
     * 住院退费确认
     * @return
     */
    @Transactional(readOnly = false)
    public int retChargeOk(Orders orders) {
        int num = 0;
//        examItemDao.deleteByApplyId(orders.getApplyId());
        List<Orders> ordersList = ordersDao.getOrders(orders);
        ordersDao.inpRetCharge("1",orders.getApplyId());
        for(Orders order: ordersList){
            InpBillDetail inpBillDetail = new InpBillDetail();
            inpBillDetail.updateOrgId();
            inpBillDetail.setOrdersId(order.getId());
            List<InpBillDetail> inpBillDetailList = inpBillDetailDao.getApplyitem(inpBillDetail);
            for(InpBillDetail detail:inpBillDetailList){
                int  amonut =  -1*(Integer.valueOf(detail.getAmount()));
                detail.setAmount(amonut+"");
                detail.setCosts(-1*(detail.getCosts()));
                detail.setCharges(-1*(detail.getCharges()));
                detail.setPerformPerson(UserUtils.getUser().getName());
                detail.preInsert();
                inpBillDetailDao.insert(detail);
            }
        }
        num = dao.inpOutpRet("2",orders.getApplyId());
        return num;
    }
}